﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ERPStore.Extensions;

using CloudSoft.Extensions;

namespace ERPStore.Indexers.Lucene.Services
{
	public class FileCompensatorRepository : ICompensatorRepository
	{
		public FileCompensatorRepository(Models.Settings settings)
		{
			FileName = settings.CompensatorFileName;
		}

		public string FileName { get; set; }

		#region ICompensatorRepository Members

		public string GetCompensation(string input)
		{
			if (!FileName.IsNullOrTrimmedEmpty())
			{
				var categoryCompensationListFileInfo = new System.IO.FileInfo(FileName);
				if (!categoryCompensationListFileInfo.Exists)
				{
					return null;
				}
				using (var content = categoryCompensationListFileInfo.OpenText())
				{
					while (true)
					{
						var line = content.ReadLine();
						if (line.IsNullOrTrimmedEmpty())
						{
							break;
						}
						string[] compensation = line.Split(':');
						var badword = compensation[0];
						if (badword.Equals(input, StringComparison.InvariantCultureIgnoreCase))
						{
							return compensation[1];
						}
					}
					content.Close();
				}
			}
			return null;
		}

		#endregion
	}
}
